# todo_api 冒烟测试脚本
import requests
import hashlib
import time
def make_md5(text):
    # 创建一个 md5加密工具
    m = hashlib.md5()
    m.update(str(text).encode(encoding='utf-8'))
    res = m.hexdigest()  # 生成加密后的内容
    return res

username = f'dh{int(time.time())}'
email = username + '@gmail.com'

s = requests.session()

# 注册
mydata = {
    "username": username,
    "password": "qwer123",
    "cpassword": "qwer123",
    "checkcode": "1234",
    "email": email
}
res = s.post(url='http://114.132.200.42:3456/user/register', data=mydata)
print(res.json())
# 登录
ret = mydata['username']
print(ret)
data = {
    "username": ret,
    "password": make_md5('qwer123'),
    "checkcode": "1234"
}
url = 'http://114.132.200.42:3456/user/login'
res1 = s.post(url=url, data=data)
print(res1.json())
result = res1.json()
getuserid = result['userId']
getusertoken = result['userToken']
print(getuserid)


# 创建任务
CreateUrl = 'http://114.132.200.42:3456/task/info'
mydata = {"title": "任务123321"}
hd = {"userId": getuserid, "userToken": getusertoken}
res2 = s.post(url=CreateUrl, data=mydata, headers=hd)  # headers 参数 表示 头部信息
print(res2.json())

result1 = res2.json()
gettaskid = result1['taskid']
print(gettaskid)
# 查询任务
GetUrl = 'http://114.132.200.42:3456/task/info'
mydata = {"status": "0"}
res3 = s.get(url=GetUrl, headers=hd, params=mydata) # get请求 params 参数 表示 请求数据
print(res3.json())
# 完成任务
completeurl = 'http://114.132.200.42:3456/task/info'
complete_data = {"task_id": gettaskid}
complete = s.put(url=completeurl, headers=hd, data=complete_data)
print(complete.json())

# 取消完成任务
cancelurl = 'http://114.132.200.42:3456/task/info'
cancel_data = {"task_id": gettaskid}
cancel = s.put(url=completeurl, headers=hd, data=cancel_data)
print(cancel.json())
# 删除任务
deleteurl = 'http://114.132.200.42:3456/task/info'
delete_data = {"task_id":gettaskid}
delete = s.delete(url=deleteurl, headers=hd, data=delete_data)
print(delete.json(),"删除成功")
# 上传头像
imgUrl = 'http://114.132.200.42:3456/user/uploal_action'
myimg = [
    ('pic',('123.png', open('123.png','rb')))
]
res8 = s.post(url=imgUrl,headers=hd,files=myimg) # 关于文件上传的接口 files 表示
print(res8.json())